<template>
  <view>
    <template v-if="managers.length > 0">
      <uni-list
        class="permission-list"
        v-for="(manager, index) in managers"
        :key="index"
      >
        <uni-list-item>
          <view slot="header" class="slot-box">
            <view class="header-text">配置人：{{ manager.nickName }}</view>
          </view>
        </uni-list-item>
        <template v-if="manager.items.length > 0">
          <view v-for="staff in manager.items" :key="staff.userId">
            <uni-list-item
              :thumb="staff.userImage"
              thumb-size="lg"
              :title="staff.userName"
            ></uni-list-item>
          </view>
        </template>
      </uni-list>
    </template>
  </view>
</template>

<script>
import uniList from "../../components/uni-list/uni-list";
import uniListItem from "../../components/uni-list-item/uni-list-item";
import uniIcons from "../../components/uni-icons/uni-icons";
import { findMyPower } from "../../common/service.js";
export default {
  components: {
    uniList,
    uniListItem,
    uniIcons,
  },
  data() {
    return {
      managers: [],
    };
  },
  onLoad(e) {
    if (e.loginInfo) {
      const loginInfo = JSON.parse(e.loginInfo);
      uni.showLoading({
        title: "加载中",
      });
      findMyPower(
        {
          userName: loginInfo.parentUserId,
          powerModule: loginInfo.powerModule,
        },
        (res) => {
          uni.hideLoading();
          if (res && res.data && res.data.code == 200) {
            if (res.data.data) {
              let list = res.data.data;
              let arr = [];
              if (list.length > 0) {
                for (let i = 0; i < list.length; i++) {
                  arr.push(this.__format(list[i]));
                }
              }
              this.managers = arr;
            }
          } else {
            uni.showToast({
              title: "获取我的权限失败",
              duration: 2000,
            });
          }
        },
        (err) => {
          uni.showToast({
            title: "获取我的权限失败",
            duration: 2000,
          });
        }
      );
    }
  },
  methods: {
    remove(managerIndex, staffIndex) {
      this.managers[managerIndex].items.splice(staffIndex, 1);
    },
    removeAll() {
      this.managers = [];
    },
    // 格式转化
    __format(item) {
      let arr = [];

      if (item.toSysDepts && item.toSysDepts.length) {
        item.toSysDepts.forEach((dept) => {
          arr.push({
            userId: dept.deptId.toString(),
            userImage: "../../static/dept.png",
            userName: dept.deptName,
            type: "department",
            checked: false,
          });
        });
      }

      if (item.toSysUsers && item.toSysUsers.length) {
        item.toSysUsers.forEach((user) => {
          arr.push({
            userId: user.userName.toString(),
            userImage: user.avatar,
            userName: user.nickName,
            type: "employee",
            checked: false,
          });
        });
      }

      return {
        parentUserId: item.parentUserId,
        toPowerId: item.toPowerId,
        userName: item.userName,
        avatar: item.avatar,
        nickName: item.nickName,
        items: arr,
      };
    },
  },
};
</script>

<style scoped>
page {
  background: #f6f6f6;
}
.permission-list {
  margin-bottom: 30upx;
}
</style>
